/*
 * 文件名：FuzzyQuerController.java
 * 版权：Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： FuzzyQuerController.java
 * 修改人：yanshu
 * 修改时间：2016年1月14日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.feichuang.chineseWoodCarving.business.ICompanyBusiness;
import com.feichuang.chineseWoodCarving.business.IPostBusiness;
import com.feichuang.chineseWoodCarving.business.IndustryTeacherBusiness;
import com.feichuang.chineseWoodCarving.model.MCompanyVo;
import com.feichuang.chineseWoodCarving.model.MIndustryTeacher;
import com.feichuang.chineseWoodCarving.model.MPagerRequestVo;
import com.feichuang.chineseWoodCarving.model.MPagerResultVo;
import com.feichuang.chineseWoodCarving.model.MPost;
import com.google.gson.Gson;

/**
 * TODO 首页模糊查询处理控制器.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Controller
@RequestMapping(value = "/FuzzyQuerHandle")
public class FuzzyQuerController {

    /**
     * 添加字段注释.
     */
    @Resource(name = "postBusiness")
    private IPostBusiness postBusiness;

    /**
     * 添加字段注释.
     */
    @Resource(name = "companyBusiness")
    private ICompanyBusiness companyBusiness;

    /**
     * 添加字段注释.
     */
    @Resource(name = "industryTeacherBusiness")
    private IndustryTeacherBusiness industryTeacherBusiness;

    /**
     * 
     * TODO 首页.
     * 
     * @param request
     *            请求
     * @param response
     *            响应
     * @param param
     *            检索参数
     * @param type
     *            页面检索类型 1:企业信息，2:供应，3:需求，4:名师
     * @return 检索结果 json
     */
    @RequestMapping(value = "/FuzzyQuer", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String fuzzyQuer(HttpServletRequest request, HttpServletResponse response, String param, int type, int currentpage, int size) {
        Gson gson = new Gson();
        Response<MPagerResultVo<?>> res = new Response<MPagerResultVo<?>>();
        param = request.getParameter("param");
        try {
            switch (type) {
                case 1:
                    queryCompany(param, res, currentpage, size);
                    break;
                case 2:
                    queryPostInfo(param, res, 0, currentpage, size);
                    break;
                case 3:
                    queryPostInfo(param, res, 1, currentpage, size);
                    break;
                case 4:
                    queryTeacher(param, res, currentpage, size);
                    break;
                default:
                    res.setCode(StatusCode.PARAMETER_ERROR);
                    res.setMessage("请求错误");
                    break;
            }

        } catch (Exception e) {
            res.setCode(StatusCode.ERROR);
            res.setMessage("风速过大");
        }
        return gson.toJson(res);
    }

    /**
     * TODO 名师查询.
     * 
     * @param param
     *            名师姓名
     * @param res
     * @param size
     * @param currentpage
     */
    private void queryTeacher(String param, Response<MPagerResultVo<?>> res, int currentpage, int size) {
        MIndustryTeacher industryTeacher = new MIndustryTeacher();
        industryTeacher.setTeacherName(param);
        MPagerRequestVo<MIndustryTeacher> pageRequest = new MPagerRequestVo<MIndustryTeacher>();
        pageRequest.setCurrentpage(currentpage);
        pageRequest.setSize(size);
        pageRequest.setDataModel(industryTeacher);
        MPagerResultVo<MIndustryTeacher> pagerResult = industryTeacherBusiness.selectMIndustryTeacherFuzzyQuer(pageRequest);
        res.setContent(pagerResult);
    }

    /**
     * TODO 企业信息查询.
     * 
     * @param param
     *            企业名称
     * @param res
     * @param size
     * @param currentpage
     */
    private void queryCompany(String param, Response<MPagerResultVo<?>> res, int currentpage, int size) {
        MCompanyVo companyVo = new MCompanyVo();
        companyVo.setEnterpriseName(param);
        MPagerResultVo<MCompanyVo> pagerResult = companyBusiness.selectMCompanyVoFuzzyQuer(companyVo, currentpage, size);
        res.setContent(pagerResult);
    }

    /**
     * TODO 查询供应/求购信息.
     * 
     * @param param
     *            供求简介
     * @param res
     * @param postType
     * @param size
     * @param currentpage
     */
    private void queryPostInfo(String param, Response<MPagerResultVo<?>> res, int postType, int currentpage, int size) {
        MPost post = new MPost();
        post.setPostType(postType);
        post.setPostTitle(param);
        MPagerResultVo<MPost> pageRes = postBusiness.selectPostFuzzyQuer(post, currentpage, size);
        res.setCode(StatusCode.SUCCESS);
        res.setContent(pageRes);
    }
}
